//
//  CTCOfflineSwitchView.m
//  biz
//
//  Created by zhangfuwei on 2017/3/29.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "CTCOfflineSwitchView.h"
#import <Masonry.h>

@implementation CTCOfflineSwitchView{
    CTCOfflineSwitchViewSwitchBlock  _switchBlock;
    UIImageView     *_backGroudImgV;
    UIButton        *_leftBtn;
    UIButton        *_rightBtn;
    UIView          *_leftRedPoint;
    UIView          *_rightRedPoint;
    BOOL            _isOnline;
}

- (instancetype)initWithSwitchBlock:(CTCOfflineSwitchViewSwitchBlock)switchBlock
{
    self = [super init];
    if (self) {
        _switchBlock    = switchBlock;
        _isOnline      = YES;
        [self loadUI];
    }
    return self;
}

- (void)loadUI {
    
    _backGroudImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ccLiveRoom_sliderBg_1"]];
    _backGroudImgV.layer.cornerRadius = 15;
    _backGroudImgV.clipsToBounds = YES;
    
    _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_leftBtn setTitle:@"线上咨询" forState:UIControlStateNormal];
    [_rightBtn setTitle:@"线下咨询" forState:UIControlStateNormal];
    
    _leftBtn.tag = 1;
    _rightBtn.tag = 2;
    
    _leftBtn.titleLabel.font = CHINESE_SYSTEM(13);
    _rightBtn.titleLabel.font = CHINESE_SYSTEM(13);
    
    [_leftBtn addTarget:self action:@selector(switchChatOrAnswerWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [_rightBtn addTarget:self action:@selector(switchChatOrAnswerWithButton:) forControlEvents:UIControlEventTouchUpInside];
    
    _leftRedPoint       = [[UIView alloc] init];
    _leftRedPoint.backgroundColor = [UIColor redColor];
    _leftRedPoint.layer.cornerRadius = 5;
    _leftRedPoint.clipsToBounds = YES;
    _leftRedPoint.hidden = YES;
    
    _rightRedPoint       = [[UIView alloc] init];
    _rightRedPoint.backgroundColor = [UIColor redColor];
    _rightRedPoint.layer.cornerRadius = 5;
    _rightRedPoint.clipsToBounds = YES;
    _rightRedPoint.hidden = YES;
    
    [self switchButtonBackGroudViewWithTag:1];
    
    [self addSubview:_backGroudImgV];
    [self addSubview:_leftBtn];
    [self addSubview:_rightBtn];
    [self insertSubview:_leftRedPoint aboveSubview:_leftBtn];
    [self insertSubview:_rightRedPoint aboveSubview:_rightBtn];



}

-(void)layoutSubviews {
    [super layoutSubviews];

    [_backGroudImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.right.equalTo(_rightBtn.mas_left);
    }];
    
    [_leftRedPoint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_leftBtn);
        make.right.equalTo(_leftBtn).offset(-1*10);
        make.height.width.equalTo(@10);
    }];
    
    [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(self);
        make.width.equalTo(_leftBtn);
    }];
    [_rightRedPoint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(_rightBtn);
        make.right.equalTo(_rightRedPoint).offset(-1*10);
        make.height.width.equalTo(@10);
    }];

}

- (void)switchChatOrAnswerWithButton:(UIButton *)btn {
    switch (btn.tag) {
        case 1:{
            
            _isOnline = YES;
            [self switchButtonBackGroudViewWithTag:btn.tag];
        }
            break;
        case 2:{
            
            _isOnline = NO;
            [self switchButtonBackGroudViewWithTag:btn.tag];
            
        }
            break;
   
        default:
            break;
    }

}

- (void)switchButtonBackGroudViewWithTag:(NSInteger)tag {
    
    _switchBlock ? _switchBlock(tag) : nil;
    [_leftBtn setBackgroundColor:_isOnline ? [UIColor whiteColor]:nil];
    [_rightBtn setBackgroundColor:!_isOnline ? [UIColor whiteColor]:nil];
    
    [_leftBtn setTitleColor:_isOnline ?HEXCOLOR(0x2e8ce5):[UIColor whiteColor] forState:UIControlStateNormal];
    [_rightBtn setTitleColor:!_isOnline ?HEXCOLOR(0x2e8ce5):[UIColor whiteColor] forState:UIControlStateNormal];
    [self requestRedBadge];
}

- (void)requestRedBadge
{
    [RequestTool POST:kTabBarRedBadgeUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"kTabBarRedStatusURL---responseObject %@", responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
        
            _leftRedPoint.hidden = ![responseObject[@"data"][@"oneRed"]  integerValue];
            _rightRedPoint.hidden = ![responseObject[@"data"][@"lineDownRed"]  integerValue];
            
        } else {
            [MBProgressHUD showInfoMessage:responseObject[@"des"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"error %@", error);
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
            [MBProgressHUD showErrorMessage:@"网络不给力"];
        }
        else {
            [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        }
    }];
}

@end
